package org.bsplus.client.mvp;

import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.Observable;
import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.Place;
import net.customware.gwt.presenter.client.place.PlaceRequest;
import net.customware.gwt.presenter.client.widget.WidgetDisplay;
import net.customware.gwt.presenter.client.widget.WidgetPresenter;

import org.bsplus.client.event.SetCentreComponentEvent;
import org.bsplus.client.event.SetCentreComponentEventHandler;

import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import org.bsplus.client.event.InitCreateIssueEvent;
import org.bsplus.client.event.InitListIssueEvent;

public class MainPresenter extends WidgetPresenter<MainPresenter.Display>
		implements SetCentreComponentEventHandler
{
	public interface Display extends WidgetDisplay
	{
		void setCentreComponent(Widget widget);

                Observable addMenuItem(String name);
	}

        @Inject
	public MainPresenter(Display display, EventBus eventBus)
	{
		super(display, eventBus);
		eventBus.addHandler(SetCentreComponentEvent.TYPE, this);
	}

	@Override
	protected void onBind()
	{
            Observable createIssueButton = getDisplay().addMenuItem("Create Issue");
            createIssueButton.addListener(Events.Select, new Listener<ComponentEvent>()
            {
                @Override
                public void handleEvent(ComponentEvent ce)
                {
                    eventBus.fireEvent(new InitCreateIssueEvent());
                }
            });

            Observable listIssueButton = getDisplay().addMenuItem("List Issues");
            listIssueButton.addListener(Events.Select, new Listener<ComponentEvent>()
            {
                @Override
                public void handleEvent(ComponentEvent ce)
                {
                    eventBus.fireEvent(new InitListIssueEvent());
                }
            });

            eventBus.fireEvent(new InitListIssueEvent());
	}

	@Override
	protected void onUnbind()
	{
	}

	@Override
	public Place getPlace()
	{
		return null;
	}

	@Override
	protected void onPlaceRequest(PlaceRequest request)
	{

	}

	@Override
	public void refreshDisplay()
	{
	}

	@Override
	public void revealDisplay()
	{
	}

	@Override
	public void setCenterComponent(SetCentreComponentEvent event)
	{
		getDisplay().setCentreComponent(event.getWidget());
	}
}
